//
//  graduallyProgress.m
//  ChengHuiTong
//
//  Created by ypwl on 15/12/1.
//  Copyright © 2015年 张文权. All rights reserved.
//

#import "graduallyProgress.h"

@implementation graduallyProgress

-(instancetype)initWithFrame:(CGRect)frame WithProgress:(CGFloat)progress{
    
    self=[super initWithFrame:frame];
    if (self) {
        
        

        _progress=progress;
        
        
    }
    
    return self;
}



- (void)setProgress:(CGFloat)progress
{
    if (progress < 0) {
        progress = 0;
    }
    if (progress > 1) {
        progress = 1;
    }
    

        _progress = progress;
        
        CGFloat maskWidth = progress * self.width;
        
        self.mask.frame = CGRectMake(0, 0, maskWidth, self.height);

}


- (void)initLayer
{
    if (self.gradLayer == nil) {
        
        self.gradLayer = [CAGradientLayer layer];
        
        self.gradLayer.frame = self.bounds;
    }
    
    self.gradLayer.startPoint = CGPointMake(0, 0.5);
    
    self.gradLayer.endPoint = CGPointMake(1, 0.5);
    

    
    NSArray *colors = @[(id)[UIColorFromString(@"#e4ef06") CGColor],
                        (id)[UIColorFromString(@"#e8d310") CGColor],
                        (id)[UIColorFromString(@"#efaa1f") CGColor],
                        (id)[UIColorFromString(@"#f96a36") CGColor]];
    
    
    [self.gradLayer setColors:colors];
    

    
    self.mask = [CALayer layer];
    
    [self.mask setFrame:CGRectMake(self.gradLayer.frame.origin.x, self.gradLayer.frame.origin.y,self.progress * self.width, self.height)];
    
  self.mask.borderColor = [UIColorFromString(@"#e4ef06") CGColor];
    
    self.mask.borderWidth = self.frame.size.height;
    self.mask.cornerRadius =self.height/2.0;
    self.mask.masksToBounds = YES;

    [self.gradLayer setMask:self.mask];
    
    [self.layer addSublayer:self.gradLayer];
}


@end
